	package view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import battleModel.BattleModel;
import battleModel.Tank;

/**
 * The main JFrame of the program. Contains the main method that runs the program. 
 * This class creates a new frame that will contain all panels that extend MasterViewPanel
 * 
 * @author Kyle Bush
 *
 */
@SuppressWarnings("serial")
public class MasterView extends JFrame {

	private MasterViewPanel currentPanel;
	private Stack<MasterViewPanel> panels;
	private JPanel body;
	
	private Map<String, ImageIcon> icons;
	private final int width = 832, height = 640;
	private int screenWidth, screenHeight;

	public JMenuBar jmb;
	private String imageListFile = "texts/images.txt";

	public static void main(String[] args) {
		new MasterView();
	}

	public MasterView() {
		createImageMap(icons);
			
		this.sizeWindow();
		this.setUpMenuBar();
		this.setDefaultPanel();
		this.setBackground(Color.BLACK);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
		this.requestFocus();
	}
	
	private void createImageMap(Map<String, ImageIcon> icons2) {
		//Text file of all button image locations
		Scanner readFile = null;
		try {
			readFile = new Scanner(new File(imageListFile));
		} catch (FileNotFoundException e) {
		}
		
		//Map of all button images!
		icons = new HashMap<String,ImageIcon>();
		String temp,key;
		while(readFile.hasNext()) {
			temp = readFile.nextLine();
			key = getKey(temp);
			icons.put(key, new ImageIcon(temp));
		}			
	}

	private void sizeWindow(){
		
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice gd = ge.getDefaultScreenDevice();

		int screenWidth = ge.getMaximumWindowBounds().width;
		int screenHeight = ge.getMaximumWindowBounds().height;

		this.setSize(width, height);
//		//Sizing Screen to Display Size
//		if (screenWidth < width || screenHeight < height) {
//			if (screenWidth < width && screenHeight < height) {
//				System.out.println("BEFORE: " + width +"\n"+height);
//				width = screenWidth;
//				height = screenHeight;
//				System.out.println("AFTER: " + width +"\n"+height);
//				this.setSize(width, height);
//			} else if (screenHeight < height) {
//				height = screenHeight;
//				this.setSize(width, screenHeight);
//			} else {
//				width = screenWidth;
//				this.setSize(screenWidth, height);
//			}
//		} else
//			this.setSize(width, height);
//		
		//Place window center of computer display
		this.setLocation(((screenWidth - width) / 2),
				((screenHeight - height) / 2));
	}

	private void setUpMenuBar() {
		jmb = new JMenuBar();
		JMenu file = new JMenu("File");

		JMenuItem mainMenu = new JMenuItem("Main Menu");
		file.add(mainMenu);
		mainMenu.setVisible(false);
		mainMenu.addActionListener(new MainMenuListener());

		JMenuItem exit = new JMenuItem("Exit");
		file.add(exit);
		exit.addActionListener(new ExitListener());

		JMenu help = new JMenu("Help");

		JMenuItem about = new JMenuItem("About");
		about.setActionCommand("ABOUT");
		about.addActionListener(new HelpListener());
		help.add(about);

		JMenuItem tradeHelp = new JMenuItem("How to Trade");
		help.add(tradeHelp);
		tradeHelp.setActionCommand("TRADE");
		tradeHelp.addActionListener(new HelpListener());

		jmb.add(file);
		jmb.add(help);
		// this.add(jmb, BorderLayout.NORTH);
	}

	private void setDefaultPanel() {
		panels = new Stack<MasterViewPanel>();
		currentPanel = new MenuView(this);

		body = new JPanel();
		body.setLayout(new CardLayout());
		body.add(currentPanel, "MAIN");

		panels.push(currentPanel);
		this.add(body, BorderLayout.CENTER);
	}

	private class ExitListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
		}
	}

	private class MainMenuListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {

		}
	}

	private class HelpListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {

			if (arg0.getActionCommand().equals("TRADE")) {
				setUpHelpWindow("texts/TradeSim.txt", "Trade Simulator Help:");
			} else {
				setUpHelpWindow("texts/About.txt", "About JavaBoy:");
			}

		}

		private void setUpHelpWindow(String file, String tex) {

			JFrame mainFrame = new JFrame("Help");
			mainFrame.setLayout(new BorderLayout());
			mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			mainFrame.setLocation(250, 200);

			JPanel bodyPanel = new JPanel(null);
			bodyPanel.setPreferredSize(new Dimension(250, 250));

			JLabel header = new JLabel(tex);
			bodyPanel.add(header);
			header.setLocation(10, 5);
			header.setSize(new Dimension(250, 20));

			JTextArea text = new JTextArea();
			text.setEditable(false);
			text.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1,
					Color.BLACK));
			JScrollPane scroll = new JScrollPane(text);
			scroll.setSize(new Dimension(480, 340));
			scroll.setLocation(15, 30);
			scroll.setVisible(true);

			bodyPanel.add(scroll);

			mainFrame.add(bodyPanel);
			mainFrame.pack();
			mainFrame.setSize(new Dimension(510, 400));
			mainFrame.setVisible(true);
			mainFrame.requestFocus();

			String content = "";
			Scanner readFile = null;

			try {
				readFile = new Scanner(new File(file));
			} catch (FileNotFoundException e) {
			}

			while (readFile.hasNext()) {
				content += readFile.nextLine() + "\n";
			}

			text.setText(content);
			text.setCaretPosition(0);
		}
	}

	/**
	 * Changes the current panel visible.
	 * 
	 * @param v - the view to be viewed
	 * @param o - any object the view might need to operate
	 */
	public void changeView(Views v, Object o) {

		switch (v) {

		case BATTLE:
			currentPanel = new BattleView(this, new BattleModel((Tank)o));
			panels.push(currentPanel);
			body.add(currentPanel, v.name());
			CardLayout cl1 = (CardLayout) body.getLayout();
			cl1.show(body, v.name());
			break;
		case CAMPAIGN:
			currentPanel = new CampaignView(this);
			panels.push(currentPanel);
			body.add(currentPanel, v.name());
			CardLayout cl2 = (CardLayout) body.getLayout();
			cl2.show(body, v.name());
			break;
		case LAN:
			currentPanel = new LanView(this);
			panels.push(currentPanel);
			body.add(currentPanel, v.name());
			CardLayout cl3 = (CardLayout) body.getLayout();
			cl3.show(body, v.name());
			break;
		case LOAD:
			currentPanel = new LoadView(this);
			panels.push(currentPanel);
			body.add(currentPanel, v.name());
			CardLayout cl4 = (CardLayout) body.getLayout();
			cl4.show(body, v.name());
			break;
		case MENU:
			currentPanel = new MenuView(this);
			
			if(o instanceof BattleModel){
				sizeWindow();
			}
			panels.push(currentPanel);
			body.add(currentPanel, v.name());
			CardLayout cl5 = (CardLayout) body.getLayout();
			cl5.show(body, v.name());
			break;
		case PREVIOUS:
			CardLayout cl6 = (CardLayout) body.getLayout();
			JPanel temp = currentPanel;
			if(temp instanceof BattleView){
				this.sizeWindow();
			}
			panels.pop();
			currentPanel = panels.peek();
			System.out.print(currentPanel.toString());
			cl6.show(body, currentPanel.toString());
			cl6.removeLayoutComponent(temp);
			break;
		default:
			// Do nothing
		}

		for (Component c : body.getComponents()) {
			if (c == currentPanel) {
				c.requestFocusInWindow();
			}
		}
	}
	
	public ImageIcon getIcon(String key){ 
		return icons.get(key);
	}
	
	private String getKey(String file){
		return file.substring(15,file.length() - 4);
	}
	
	public Dimension getDisplayDimension(){
		return new Dimension(screenWidth, screenHeight);
	}
	/**
	 * Get the dimension of the window (dependent on display size)
	 * @return
	 */
	public Dimension getWindowDimension(){
		return new Dimension(width,height);
	}
}
